<?php
// +----------------------------------------------------------------------
// | RXThinkCMF_EVTP5.1_PRO前后端分离旗舰版框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2021 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | 作者: ZhangXuanNeng <rxthinkcmf@163.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\service;

use app\admin\model\SettlementPre;
use think\Db;

/**
 * 预结算单-服务类
 * @author ZhangXuanNeng
 * @since 2024/01/16
 * Class SettlementPreService
 * @package app\admin\service
 */
class SettlementPreService extends BaseService
{
    /**
     * 构造函数
     * @author ZhangXuanNeng
     * @since 2020/11/19
     * UserService constructor.
     */
    public function __construct()
    {
        $this->model = new SettlementPre();
    }

    /**
     * 获取数据列表
     * @return array
     * @since 2024/01/16
     * @author ZhangXuanNeng
     */
    public function getList()
    {
        $param = request()->param();

        // 查询条件
        $map = [];

        if(isset($param['promote_1_id']) && !empty($param['promote_1_id'])){
            $map[] = ['promote_1_id','=',$param['promote_1_id']];
        }
        if(isset($param['promote_id']) && !empty($param['promote_id'])){
            $map[] = ['promote_id','=',$param['promote_id']];
        }
        if(isset($param['package_id']) && !empty($param['package_id'])){
            $map[] = ['package_id','=',$param['package_id']];
        }
        if(isset($param['status']) && !empty($param['status'])){
            $map[] = ['status','=',$param['status']];
        }
        if(isset($param['jies_type']) && !empty($param['jies_type'])){
            $map[] = ['jies_type','=',$param['jies_type']];
        }
        if(isset($param['record_date']) && !empty($param['record_date'])){
            $map[] = ['record_date','between time',$param['record_date']];
        }
        if(isset($param['settlement_time']) && !empty($param['settlement_time'])){
            $map[] = ['settlement_time','between time',$param['settlement_time']];
        }
        $res = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => [],
            "count" => 0,
            "allData"=> [
                'violation_all_amount'  => 0,
                'sum_money'             => 0,
                'event_money'           => 0,
                'total_money'           => 0,
            ]
        );
        if($map){
            $res = parent::getListNewNoPage($map, "id desc");
            $violation_all_money = 0;
            foreach ($res['data'] as $k=>&$v){
                $where[] = ['record_date','=',$v['record_date']];
                $where[] = ['jies_promote_id','=',$v['promote_id']];
                $where[] = ['package_id','=',$v['package_id']];
                $where[] = ['status','=',1];
                $violation_amount = Db::name('settlement_violation')->where($where)->sum('event_money');
                $v['violation_amount'] = $violation_amount;
                $violation_remark = Db::name('settlement_violation')->where($where)->column('remark');
                $v['violation_remark'] = implode(';',$violation_remark);
                $violation_all_money += $violation_amount;
            }
            $sum_money = $this->model->where($map)->sum('sum_money');
            $event_money = $this->model->where($map)->sum('event_money');
            $total_money = $this->model->where($map)->sum('total_money');
            $res['allData']['violation_all_money'] = round($violation_all_money,2);
            $res['allData']['sum_money'] = round($sum_money,2);
            $res['allData']['event_money'] = round($event_money,2);
            $res['allData']['total_money'] = round($total_money,2);
        }

        return $res;
    }

    /**
     * 修改分成比例
     * @return mixed
     * @since 2024/01/16
     * @author ZhangXuanNeng
     */
    public function updateProportion()
    {
        $params = request()->param();
        $res = $this->model->updateProportion($params);
        return $res;
    }
    public function importEvent(){
        $param = request()->param();
        $path = \Env::get('root_path') . 'public/uploads'.$param['url'];
        require_once VENDOR_PATH . 'phpoffice/phpexcel/Classes/PHPExcel.php';
        require_once VENDOR_PATH . 'phpoffice/phpexcel/Classes/PHPExcel/IOFactory.php';
        $objWriter = \PHPExcel_IOFactory::createReader('Excel5');
        $objPHPExcel = $objWriter->load($path, 'utf-8');
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow(); // 取得总行数
        $importRows = 0;
        $data = [];
        for ($j = 2; $j <= $highestRow; $j++) {
            $importRows++;
            $record_date = (string)$objPHPExcel->getActiveSheet()->getCell("A$j")->getValue();
            $data[$importRows]['record_date'] = str_replace('.','-',$record_date);
            $data[$importRows]['promote_id'] = (string)$objPHPExcel->getActiveSheet()->getCell("B$j")->getValue();
            $data[$importRows]['package_id'] = (string)$objPHPExcel->getActiveSheet()->getCell("C$j")->getValue();
            $data[$importRows]['event_money'] = (string)$objPHPExcel->getActiveSheet()->getCell("D$j")->getValue();
            $data[$importRows]['remark'] = (string)$objPHPExcel->getActiveSheet()->getCell("E$j")->getValue();
        }
        $res = $this->model->importEvent($data);
        return $res;
    }
    /**
     * 删除记录
     * @return array
     * @since 2024/01/18
     * @author ZhangXuanNeng
     */
    public function delete()
    {
        $params = request()->param();
        $res = $this->model->del($params);
        return $res;
    }
    /**
     * 结算前计算结算信息
     * @return array
     * @since 2024/01/18
     * @author ZhangXuanNeng
     */
    public function getSettlement(){
        $params = request()->param();
        if(!isset($params['promote_id'])){
            return message('请选择结算渠道', false);
        }
        $map[] = ['id','in',$params['ids']];
        $map[] = ['status','=',1];
        $map[] = ['promote_id','=',$params['promote_id']];
        $res = $this->model->getSettlement($map,$params);
        return $res;
    }

    /**
     * 结算
     * @return array
     * @since 2024/01/18
     * @author ZhangXuanNeng
     */
    public function setSettlement(){
        $params = request()->param();
        if(!isset($params['promote_id'])){
            return message('请选择结算渠道', false);
        }
        $map[] = ['id','in',$params['ids']];
        $map[] = ['status','=',1];
        $map[] = ['promote_id','=',$params['promote_id']];
        $res = $this->model->setSettlement($map,$params);
        return $res;
    }
    /**
     * 重新生成预结算单
     * @return array
     * @since 2024/01/18
     * @author ZhangXuanNeng
     */
    public function setSettlementPre(){
        $params = request()->param();
        if(empty($params['promote_id']) || empty($params['package_id']) || empty($params['start_time']) || empty($params['end_time']) || empty($params['proportion'])){
            return message('请将信息填写完整', false);
        }
        $res = $this->model->setSettlementPre($params);
        return $res;
    }
    /**
     * 自动结算导入
     * @return array
     * @since 2024/01/19
     * @author ZhangXuanNeng
     */
    public function importQueue(){
        $param = request()->param();
        $path = \Env::get('root_path') . 'public/uploads'.$param['url'];
        require_once VENDOR_PATH . 'phpoffice/phpexcel/Classes/PHPExcel.php';
        require_once VENDOR_PATH . 'phpoffice/phpexcel/Classes/PHPExcel/IOFactory.php';
        $objWriter = \PHPExcel_IOFactory::createReader('Excel5');
        $objPHPExcel = $objWriter->load($path, 'utf-8');
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow(); // 取得总行数
        $importRows = 0;
        $data = [];
        for ($j = 2; $j <= $highestRow; $j++) {
            $importRows++;
            $data[$importRows]['start_time'] = (string)$objPHPExcel->getActiveSheet()->getCell("A$j")->getValue();
            $data[$importRows]['end_time'] = (string)$objPHPExcel->getActiveSheet()->getCell("B$j")->getValue();
            $data[$importRows]['promote_id'] = (string)$objPHPExcel->getActiveSheet()->getCell("C$j")->getValue();
            $data[$importRows]['jies_type'] = (string)$objPHPExcel->getActiveSheet()->getCell("D$j")->getValue();
        }
        $res = $this->model->importQueue($data);
        return $res;
    }
}